package com.lch.base.http.observer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;

import com.lch.base.http.callback.ICallbackBase;

import io.reactivex.disposables.Disposable;

/**
 * 有loading框的Observer
 *
 * @author lee(29487113@qq.com) on 18/2/4
 */
public class RxObserverProgress<T> extends RxObserverBase<T> {
    private ProgressDialog mProgress;

    public RxObserverProgress(ICallbackBase callBack, boolean isShowLoad, Context context) {
        super(callBack);
        if (isShowLoad && context != null && context instanceof Activity) {
            mProgress = new ProgressDialog(context);
            mProgress.setMessage("加载中....");
        }
    }


    //当订阅后，会首先调用这个方法，其实就相当于onStart()，
    @Override
    public void onSubscribe(Disposable d) {
        super.onSubscribe(d);
        showProgress();
    }

    @Override
    public void onError(Throwable e) {
        super.onError(e);
        dismissProgress();
    }

    @Override
    public void onComplete() {
        super.onComplete();
        dismissProgress();
    }

    private void showProgress() {
        if (mProgress != null) {
            if (mProgress.isShowing()) {
                mProgress.dismiss();
            }
            mProgress.show();
        }
    }

    private void dismissProgress() {
        if (mProgress != null && mProgress.isShowing()) {
            mProgress.dismiss();
        }
    }
}